//
//  MyQYaViewController.m
//  Qya
//
//  Created by maxwellpang on 12-9-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyQYaViewController.h"
#import "EmailViewController.h"
#import "FieldViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "UIView+Screenshot.h"
#import "FieldViewController.h"
#import "keywordViewController.h"
#import "FeedsDetailViewController.h"

#define EDGE_X 20
#define EDGE_Y 20
#define FIELDVIEW_SIZE_WIDTH 128
#define FIELDVIEW_SIZE_HEIGHT 136

@interface MyQYaViewController ()

@end

@implementation MyQYaViewController
@synthesize fieldViewController;
@synthesize fieldViewController1;
@synthesize fieldViewController2;
@synthesize fieldViewController3;

@synthesize mapView = _mapView;
@synthesize leftTableView = _leftTableView;
@synthesize centerTableView = _centerTableView;
@synthesize backgroundImageView = _backgroundImageView;

static MyQYaViewController *sharedKeyWordViewController;

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,240,[self.view bounds].size.height)];
        UIImageView *myYaXinBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"youce.png"]];
        [_mapView addSubview:myYaXinBackgroundView];
        [self setRightFoldContentView:_mapView rightViewFoldCount:3 rightViewPullFactor:0.9];
        
        _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beijingtupian.png"]];
        [_backgroundImageView setFrame:[self.view frame]];
        
        _centerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,[self.view bounds].size.width,[self.view bounds].size.height)];
        [self addFieldToCenterTableView];
        [_centerTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_centerTableView addSubview:_backgroundImageView];
        _backgroundImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_centerTableView];
        
        //加入种子按钮
        btSow = [[UIButton alloc]initWithFrame:CGRectMake(160,400,50,50)];
        [_backgroundImageView addSubview:btSow];
        
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,120,[self.view bounds].size.height)];
//        [_leftTableView setRowHeight:100];
        UIImageView *_leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zuoce.png"]];
        [_leftTableView addSubview:_leftImageView];
        [_leftTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self setLeftFoldContentView:_leftTableView];
        
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(-1,0,1,[self.view bounds].size.height)];
        [self.contentView addSubview:line];
        [line setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1]];
        
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake([self.view bounds].size.width,0,1,[self.view bounds].size.height)];
        [self.contentView addSubview:line2];
        [line2 setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:1]];
        
        sharedKeyWordViewController = self;
        
        //初始化数据
        NSArray *b1 = [[NSArray alloc]initWithObjects:@"FeedsA",@"咖啡", nil] ;
        NSArray *b2 = [[NSArray alloc]initWithObjects:@"FeedsB",@"打篮球", nil] ;
        NSArray *b3 = [[NSArray alloc]initWithObjects:@"FeedsC",@"iphone5", nil] ;
        mBlockArray = [[NSMutableArray alloc] initWithObjects:b1,b2,b3,nil];
        
        fieldArray = [[NSMutableArray alloc] init];
        
        
        

        NSInteger row, colum;
        int i;
        NSInteger x, y;
        CGRect rect;
        
        kwViewController = [keywordViewController sharedKeyWordViewController]; 
        
        
        i=0;
        row = i / 2;
        colum = i % 2;
        

        if (colum == 0)
            x = EDGE_X;
        else
            x = self.view.frame.size.width - EDGE_X - FIELDVIEW_SIZE_WIDTH;
        y = EDGE_Y + row * FIELDVIEW_SIZE_HEIGHT + row * EDGE_Y;
        
        fieldViewController = [[FieldViewController alloc] initWithNibName:@"FieldViewController" bundle:nil];
        
        rect = CGRectMake(x, y, FIELDVIEW_SIZE_WIDTH, FIELDVIEW_SIZE_HEIGHT);
        [fieldViewController.view setFrame:rect];
        [_backgroundImageView addSubview:fieldViewController.view];
        fieldViewController.keywordLabel.text = @"咖啡"; //[kwViewController.mSearchArray objectAtIndex:i];
        fieldViewController.number = i;
        [fieldViewController.btField setImage:[UIImage imageNamed:@"coffee"] forState:UIControlStateNormal];
        [fieldViewController.messageCountImageView setImage:[UIImage imageNamed:@"shishishuzu15fuben-1"]];
        
        
        
        i=1;
        row = i / 2;
        colum = i % 2;
        
        
        if (colum == 0)
            x = EDGE_X;
        else
            x = self.view.frame.size.width - EDGE_X - FIELDVIEW_SIZE_WIDTH;
        y = EDGE_Y + row * FIELDVIEW_SIZE_HEIGHT + row * EDGE_Y;
        
        fieldViewController1 = [[FieldViewController alloc] initWithNibName:@"FieldViewController" bundle:nil];
        fieldViewController1.keywordLabel.text = [kwViewController.mSearchArray objectAtIndex:i];
        
       rect = CGRectMake(x, y, FIELDVIEW_SIZE_WIDTH, FIELDVIEW_SIZE_HEIGHT);
        [fieldViewController1.view setFrame:rect];
        [_backgroundImageView addSubview:fieldViewController1.view];
        fieldViewController1.keywordLabel.text = @"打篮球";
        fieldViewController1.number = i;
        [fieldViewController1.btField setImage:[UIImage imageNamed:@"dalanqiu"] forState:UIControlStateNormal];
        [fieldViewController1.messageCountImageView setImage:[UIImage imageNamed:@"shishishuzu15fuben2-1"]];
        
        
        
        i=2;
        row = i / 2;
        colum = i % 2;
        
        
        if (colum == 0)
            x = EDGE_X;
        else
            x = self.view.frame.size.width - EDGE_X - FIELDVIEW_SIZE_WIDTH;
        y = EDGE_Y + row * FIELDVIEW_SIZE_HEIGHT + row * EDGE_Y;
        
        fieldViewController2 = [[FieldViewController alloc] initWithNibName:@"FieldViewController" bundle:nil];
        fieldViewController2.keywordLabel.text = [kwViewController.mSearchArray objectAtIndex:i];
        
        rect = CGRectMake(x, y, FIELDVIEW_SIZE_WIDTH, FIELDVIEW_SIZE_HEIGHT);
        [fieldViewController2.view setFrame:rect];
        [_backgroundImageView addSubview:fieldViewController2.view];
        fieldViewController2.keywordLabel.text = @"iphone5";
        fieldViewController2.number = i;
        [fieldViewController2.btField setImage:[UIImage imageNamed:@"morentufubeng"] forState:UIControlStateNormal];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"芽信" style:UIBarButtonItemStylePlain target:self action:@selector(emailAction:)];  
//    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"边栏" style:UIBarButtonItemStylePlain target:self action:@selector(menuAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
//    self.navigationItem.leftBarButtonItem = leftButton;
    
//    FieldViewController *field1 = [[FieldViewController alloc]init];
//    CGRect 
//    [field1.view setFrame:[(<#NSUInteger loc#>, <#NSUInteger len#>)
//    [self.view addSubview:field1.view]
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//展示侧边菜单
-(void)menuAction:(id)sender  
{  
    
}

-(void)emailAction:(id)sender  
{  
    EmailViewController *secondView = [[EmailViewController alloc] init];  
    [self.navigationController pushViewController:secondView animated:YES];  
    secondView.title = @"我的芽信"; 
}

- (void)addFieldToCenterTableView
{
    
    kwViewController = [keywordViewController sharedKeyWordViewController]; 
//    for (int i=3; i<[kwViewController.mSearchArray count]; ++i)
//    {
    int i=3;
    
        NSInteger row, colum;
        row = i / 2;
        colum = i % 2;
        
        NSInteger x, y;
        if (colum == 0)
            x = EDGE_X;
        else
            x = self.view.frame.size.width - EDGE_X - FIELDVIEW_SIZE_WIDTH;
        y = EDGE_Y + row * FIELDVIEW_SIZE_HEIGHT + row * EDGE_Y;
        
        fieldViewController3 = [[FieldViewController alloc] initWithNibName:@"FieldViewController" bundle:nil];

        
        CGRect rect = CGRectMake(x, y, FIELDVIEW_SIZE_WIDTH, FIELDVIEW_SIZE_HEIGHT);
        [fieldViewController3.view setFrame:rect];
        [_backgroundImageView addSubview:fieldViewController3.view];
        fieldViewController3.number = i;

    if ([kwViewController.mSearchArray count] == 4) {
        fieldViewController3.keywordLabel.text = [kwViewController.mSearchArray objectAtIndex:i];
    }
    

        [fieldViewController3.messageCountImageView setImage:nil];

//    }
}

+ (id)sharedKeyWordViewController
{
    return sharedKeyWordViewController;
}

-(void)gotoDetail:(NSInteger)number
{
    if (number > 2) {
        return;
    }
    FeedsDetailViewController *secondView = [[FeedsDetailViewController alloc] init]; 
    [self.navigationController pushViewController:secondView animated:YES];  
    
    NSArray *tmp = [mBlockArray objectAtIndex:number];
    [secondView.wholePic setImage:[UIImage imageNamed:[tmp objectAtIndex:0]]];
    
    secondView.title = [tmp objectAtIndex:1]; 
}

@end
